package com.cabrida.livestar.main;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.login.LoginActivity;
import com.cabrida.livestar.main.IndicatorMainView.OnIndicateListener;
import com.cabrida.livestar.model.FansAdvanceListData;
import com.cabrida.livestar.model.FansCallStar;
import com.cabrida.livestar.model.FansGiftData;
import com.cabrida.livestar.model.FansLiveListData;
import com.cabrida.livestar.model.FansLivePlayBackListData;
import com.cabrida.livestar.model.FansStarList;
import com.cabrida.livestar.model.FansVLogData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.StarCircleData;
import com.cabrida.livestar.pay.PayActivity;
import com.cabrida.livestar.util.DoubleClickExitHelper;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.view.GridViewExtend;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 粉丝端
 */
public class MainActivity extends BaseActivity {
	public static final String TAG = MainActivity.class.getSimpleName();
	private DoubleClickExitHelper mDoubleClickExitHelper;
	private LinearLayout layout_body_main;
	private IndicatorMainView mIndicator;
	private FansHomePageView homePageView;
	private FansStarCircleView starCircleView;
	private FansVlogView vlogView;
	private FansMineView mineView;
	
	/**
	 * 呼唤明星 兑换L币 列表
	 * 账户余额
	 */
	private ArrayList<FansGiftData> data_list = new ArrayList<FansGiftData>();
	private String u_balance;
	private Dialog dialogCallStarPay;

	/**
	 * 当前页面
	 */
	private int isFlag = IndicatorMainView.VIEW_HOMEPAGE;
	
	public static final String ACTION_HOMEPAGE_LIST = "com.cabrida.livestar_homelist";
	public static final String ACTION_VLOG_LIST = "com.cabrida.livestar_vloglist";
	public static final String ACTION_VLOG_ATTENTION_LIST = "com.cabrida.livestar_vlogattention";
	public static final String ACTION_STARCIRCLE_LIST = "com.cabrida.livestar_startcirclelist";
	public static final String ACTION_STARCIRCLELIKE_LIST = "com.cabrida.livestar_startcirclelist_like";
	public static final String ACTION_ADVANCE_REFRESH = "com.cabrida.livestar_advance_refresh";
	public static final String ACTION_STARLIST = "com.cabrida.livestar_starlist";
	public static final String ACTION_PAYMENT = "com.cabrida.livestar_starlist_pay";
	public static final String ACTION_QUIT = "com.cabrida.livestar_quit";
	public static final String ACTION_EDIT_INFORMATION = "com.cabrida.livestar_edit_information";
	public static final String ACTION_HOMEPAGE_CALLSTAR = "com.cabrida.livestar_homepage_callstar";

	private BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(ACTION_HOMEPAGE_LIST)) {
				getHomePageList();
			}
			if (intent.getAction().equals(ACTION_STARCIRCLE_LIST)) {
				getCircleList();
			}
			if (intent.getAction().equals(ACTION_STARCIRCLELIKE_LIST)) {
				String cid  = intent.getStringExtra("cid");
				btnLike(cid);
			}
			if (intent.getAction().equals(ACTION_VLOG_LIST)) {
				getVLogList();
			}
			if (intent.getAction().equals(ACTION_VLOG_ATTENTION_LIST)) {
				String s_udid  = intent.getStringExtra("s_udid");
				if (null!=vlogView) {
					getVLogAttentionBtn(s_udid);
				}
			}
			if (intent.getAction().equals(ACTION_ADVANCE_REFRESH)) {
				String follow  = intent.getStringExtra("follow");
				getAdvancedata(follow);
			}
			if (intent.getAction().equals(ACTION_QUIT)) {
				QuitLogin();
			}
				if (intent.getAction().equals(ACTION_STARLIST)) {
				int position  = intent.getIntExtra("position", 0);
				if (position==0) {
					//直播列表
					getLiveList();
				}else  if (position==1) {
					//预告列表
					getAdvanceList();
				}else {
					//其他列表
					getStarList(position,intent.getStringExtra("value"));
				}
				
			}
			
			if (intent.getAction().equals(ACTION_PAYMENT)) {
				FansLiveListData data = (FansLiveListData) intent.getSerializableExtra("data");
				getPayMent(data);
			}
			if (intent.getAction().equals(ACTION_EDIT_INFORMATION)) {
				mineView.refreshPersonalData();
			}
			if (intent.getAction().equals(ACTION_HOMEPAGE_CALLSTAR)) {
				String s_id  = intent.getStringExtra("s_id");
				getCallStarBtn(s_id);
			}
		}
	};
	
	/** 获取呼唤明星  获取次数列表 */
	private void getCallStarBtn(final String s_id) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("shout", s_id);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CALLSTARBTN_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getCallStarBtn", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data, "message", "");
							showToast(message);
							getHomePageList();
						} else if(code == HttpHelp.Failure){
							getPayNumberList();
						}else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data, "message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取次数列表 */
	private void getPayNumberList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("type", "250");
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_GIFT_LIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getPayNumberList", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data,
									"list", new JSONArray());
							u_balance = JsonUtils.getString(data,
									"u_balance", "");
							data_list = JSONParser.getFansCallStarConvertList(list);
							popCallStarWindows();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/**
	 * 购买次数
	 */
	public void popCallStarWindows() {
		showCallStarDialog();
	}

	private void showCallStarDialog() {
		initCallStarDialogView();
		if (null != dialogCallStarPay && dialogCallStarPay.isShowing()) {
			return;
		}
		if (null != dialogCallStarPay) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogCallStarPay.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogCallStarPay.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogCallStarPay.setCanceledOnTouchOutside(true);
			dialogCallStarPay.show();
		}
	}

	private void initCallStarDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater.inflate(R.layout.dialog_callstar_pay, null);
		ImageButton ib_close = (ImageButton) view.findViewById(R.id.ib_close);
		ib_close.setOnClickListener(this);
		LinearLayout ll_goto_pay = (LinearLayout) view.findViewById(R.id.ll_goto_pay);
		TextView tv_pay = (TextView) view.findViewById(R.id.tv_pay);
		TextView tv_account = (TextView) view.findViewById(R.id.tv_account);
		tv_account.setText(u_balance);
		ll_goto_pay.setOnClickListener(this);
		tv_pay.setOnClickListener(this);
		GridViewExtend gridView_pay = (GridViewExtend) view.findViewById(R.id.gridView_pay);
		FansCallStarPayGvAdapter adapter_pay = new FansCallStarPayGvAdapter(context);
		gridView_pay.setAdapter(adapter_pay);
		adapter_pay.setData(data_list);
		adapter_pay.notifyDataSetChanged();

		dialogCallStarPay = new Dialog(context, R.style.dialogWindowStyle);
		dialogCallStarPay.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogCallStarPay.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}
	
	/** 获取订单数据
	 * type 10wx 20ali 30apple 40支付次数
	 *  */
	private void getOrderData(String cid,String amount) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("type", "40");
		params.put("cid", cid);
		params.put("amount", amount);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_PAY_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getOrderData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	private void QuitLogin(){
		clearData();
		this.finish();
		openActivity(LoginActivity.class);
	}
	
	/** 点赞 */
	private void btnLike(final String cid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", cid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_LIKE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "btnLike", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							String number = JsonUtils.getString(data,"number", "");
							starCircleView.putLikeData(cid, number);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取明星列表 */
	private void getStarList(final int position,String value) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("value", "20");
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARLIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "starlist_response", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONArray data = JsonUtils.getJSONArray(response, "data", new JSONArray());
							ArrayList<FansStarList> infoList = JSONParser.getFansStarList(data);
//							homePageView.notifyDataOther(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 点击直播列表，支付接口 */
	private void getPayMent(final FansLiveListData data) {
		final String channel = data.getL_channel().toString();
		final String price = data.getS_price().toString();
//		final String GroupId = data.getL_group().toString();
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("channel", channel);
		params.put("price", price);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_PAYMENT_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "payment_response", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							homePageView.popWindows(data,FansHomePageView.PAY_TYPE_0);
						} else if(code == HttpHelp.Failure){
							
							
							JSONObject data1 = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data1,"message", "");
							if ("直播已结束".equals(message)) {
								homePageView.popWindows(data,FansHomePageView.PAY_TYPE_2);
								getHomePageList();
							}else {
								homePageView.popWindows(data,FansHomePageView.PAY_TYPE_1);
							}
						}else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取首页数据 */
	private void getHomePageList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 1);
		params.put("live", 6);
		params.put("advance", 3);
		params.put("play", 4);
		params.put("shout", 6);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_HOMELIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getHomePageList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONObject live = JsonUtils.getJSONObject(data, "live", new JSONObject());
							JSONArray list_live = JsonUtils.getJSONArray(live, "list", new JSONArray());
							JSONObject advance = JsonUtils.getJSONObject(data, "advance", new JSONObject());
							JSONArray list_advance = JsonUtils.getJSONArray(advance, "list", new JSONArray());
							JSONObject play = JsonUtils.getJSONObject(data, "play", new JSONObject());
							JSONArray list_play = JsonUtils.getJSONArray(play, "list", new JSONArray());
							JSONObject shout = JsonUtils.getJSONObject(data, "shout", new JSONObject());
							JSONArray list_shout = JsonUtils.getJSONArray(shout, "list", new JSONArray());
							ArrayList<FansLiveListData> infoList_live = JSONParser.getFansLiveList(list_live);
							ArrayList<FansAdvanceListData> infoList_advance = JSONParser.getFansAdvanceList(list_advance);
							ArrayList<FansLivePlayBackListData> infoList_playback = JSONParser.getFansLivePlayBackList(list_play);
							ArrayList<FansCallStar> infoList_shout = JSONParser.getFansCallStarList(list_shout);
							homePageView.setLvData(infoList_live,infoList_advance,infoList_playback,infoList_shout);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 星部落点击关注数据 */
	private void getVLogAttentionBtn(final String s_udid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("follow", s_udid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_VLOG_ATTENTION_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getVLogAttentionBtn", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							String status=JsonUtils.getString(data,"status", "");
							String number=JsonUtils.getString(data,"number", "");
							String num=JsonUtils.getString(data,"num", "");
							showToast(message);
							vlogView.attentionData(status,s_udid,number,num);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取星友圈数据 */
	private void getCircleList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARCIRCLELIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getCircleList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<StarCircleData> infoList = JSONParser.getFansCircleList(list);
							starCircleView.putData(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取星部落数据 */
	private void getVLogList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARLIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getVLogList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String subscribe = JsonUtils.getString(data, "subscribe", "");
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<FansVLogData> infoList = JSONParser.getFansVLogList(list);
							vlogView.putData(infoList,subscribe);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取预播改变订阅状态 */
	private void getAdvancedata(final String follow) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("follow", follow);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_BOOK_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAdvancedata", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							homePageView.setAdvanceBook(follow);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取预播列表 */
	private void getAdvanceList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 0);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_ADVANCE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "advancelist_response", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						
						//
						if (code == HttpHelp.Succeed) {
							JSONArray data = JsonUtils.getJSONArray(response, "data", new JSONArray());
							ArrayList<FansAdvanceListData> infoList = JSONParser.getFansAdvanceList(data);
							FansAdvanceListData data1 = infoList.get(0);
							if (null!=infoList&&infoList.size()>0) {
								infoList.add(data1);
								infoList.add(data1);
								infoList.add(data1);
								infoList.add(data1);
								infoList.add(data1);
								infoList.add(data1);
								infoList.add(data1);
							}
//							homePageView.notifyDataAdvance(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取直播列表 */
	private void getLiveList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 0);
		params.put("status", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_LIVE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "livelist_response", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONArray data = JsonUtils.getJSONArray(response, "data", new JSONArray());
							ArrayList<FansLiveListData> infoList = JSONParser.getFansLiveList(data);
//							homePageView.notifyDataLive(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_main);

		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

		Global.Screen_width = displaymetrics.widthPixels;
		Global.Screen_height = displaymetrics.heightPixels;

		layout_body_main = (LinearLayout) findViewById(R.id.layout_body_main);
		
		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(ACTION_HOMEPAGE_LIST);
		filter_ad.addAction(ACTION_VLOG_LIST);
		filter_ad.addAction(ACTION_VLOG_ATTENTION_LIST);
		filter_ad.addAction(ACTION_STARCIRCLE_LIST);
		filter_ad.addAction(ACTION_STARCIRCLELIKE_LIST);
		filter_ad.addAction(ACTION_ADVANCE_REFRESH);
		filter_ad.addAction(ACTION_STARLIST);
		filter_ad.addAction(ACTION_PAYMENT);
		filter_ad.addAction(ACTION_QUIT);
		filter_ad.addAction(ACTION_EDIT_INFORMATION);
		filter_ad.addAction(ACTION_HOMEPAGE_CALLSTAR);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mDoubleClickExitHelper = new DoubleClickExitHelper(this);
		mIndicator = (IndicatorMainView) findViewById(R.id.indicator);
		addViewBody(isFlag);
		mIndicator.setIndicator(isFlag);
		mIndicator.setOnIndicateListener(new OnIndicateListener() {
			@Override
			public void onIndicate(View v, int which) {
				addViewBody(which);
			}
		});
	}

	/**
	 * 动态添加布局
	 */
	private void addViewBody(int flag) {
		isFlag = flag;
		switch (flag) {
		case IndicatorMainView.VIEW_HOMEPAGE:
//			if (null == homePageView) {
				homePageView = new FansHomePageView(context);
//			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(homePageView);
			break;

		case IndicatorMainView.VIEW_STAR_CIRCLE:
//			if (null == starCircleView) {
				starCircleView = new FansStarCircleView(context);
//			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(starCircleView);
			break;
		case IndicatorMainView.VIEW_VLOG:
//			if (null == vlogView) {
				vlogView = new FansVlogView(context);
//			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(vlogView);
			break;

		case IndicatorMainView.VIEW_MINE:
//			if (null == mineView) {
				mineView = new FansMineView(context);
//			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(mineView);
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.tv_pay:
			boolean ischeck=false;
			String cid="";
			String amount="";
			for (int i = 0; i < data_list.size(); i++) {
				if (data_list.get(i).isCheck()) {
					ischeck = true;
					cid = data_list.get(i).getCid();
					amount = data_list.get(i).getC_value();
					break;
				}
			}
			if (!ischeck) {
				showToast("请选择兑换次数");
				break;
			}
			getOrderData(cid, amount);
			if (null != dialogCallStarPay) {
				dialogCallStarPay.dismiss();
			}
			break;
		case R.id.ll_goto_pay:
			if (null != dialogCallStarPay) {
				dialogCallStarPay.dismiss();
			}
			Intent intent = new Intent();
			intent.setClass(context, PayActivity.class);
			context.startActivity(intent);
			break;
		case R.id.ib_close:
			if (null != dialogCallStarPay) {
				dialogCallStarPay.dismiss();
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			return mDoubleClickExitHelper.onKeyDown(keyCode, event);
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
}
